package com.settlement.system.common.util.common;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.Map;

/**
 * @author author
 */
public class FasUtil {

	/**
	 * 左圆括号
	 */
	private static final int OPEN_PARENTHESIS = '(';

	/**
	 * A,B,C - ('A','B','C')/A;B;C - ('A','B','C')
	 * @param str
	 * @return
	 */
	public static String formatInQueryConditionByChar(String str,String strChar){
		if(StringUtils.isNotBlank(str) && str.indexOf(OPEN_PARENTHESIS)!= 0){
			str =str.replaceAll(strChar, "','");
			str = "('".concat(str).concat("')");
		}
		return str;
	}
	
	/**
	 * A,B,C - ('A','B','C')
	 * @param str
	 * @return
	 */
	public static String formatInQueryCondition(String str){
		if(StringUtils.isNotBlank(str) && str.indexOf(OPEN_PARENTHESIS)!= 0){
			str =str.replaceAll(",", "','");
			str = "('".concat(str).concat("')");
		}
		return str;
	}
	
	/**
	 * A,B,C - 'A','B','C'
	 * @param str
	 * @return
	 */
	public static String formatInCondition(String str){
		if(StringUtils.isNotBlank(str) && str.indexOf(OPEN_PARENTHESIS)!= 0){
			str =str.replaceAll(",", "','");
			str = "'".concat(str).concat("'");
		}
		return str;
	}
	
	/**
	 * ('A','B','C')- A,B,C 
	 * @param str
	 * @return
	 */
	public static String parseInQueryCondition(String str){
		if(StringUtils.isNotBlank(str) && str.indexOf(OPEN_PARENTHESIS)== 0){
			str = str.replaceAll("'", "");
			str = str.substring(1, str.length() - 1);
		}
		return str;
	}
	
	/**
	 * 'A','B','C'- A,B,C 
	 * @param str
	 * @return
	 */
	public static String splitStr(String str){
		String resultStr = "";
		if(StringUtils.isNotBlank(str)){
			resultStr = str.replaceAll("'", "");
			resultStr = resultStr.substring(0, resultStr.length());
		}
		return resultStr;
	}
	
	/**
	 * 'A','B','C'- ('A','B','C') 
	 * @param str
	 * @return
	 */
	public static String splitToInStr(String str){
		String resultStr = "";
		if(StringUtils.isNotBlank(str)){
			resultStr = "(".concat(str).concat(")");
		}
		return resultStr;
	}
	
	/**
	 * ('A','B','C')- 'A','B','C'
	 * @param str
	 * @return
	 */
	public static String parseInCondition(String str){
		String resultStr = "";
		if(StringUtils.isNotBlank(str)){
			resultStr = str.substring(1, str.length() - 1);
		}
		return resultStr;
	}

	public static void formatInQueryCondition(Map<String,Object> params,String... fileds){
		if(CollectionUtils.isEmpty(params)){
			return;
		}
		for(int index = 0;index < fileds.length;index++){
			String key = fileds[index];
			Object object = params.get(key);
			if(null != object){
				params.put(key,formatInQueryCondition(object.toString()));
			}
		}
	}

    public static void toUpperCase(Map<String,Object> params,String... fileds){
        if(CollectionUtils.isEmpty(params)){
            return;
        }
        for(int index = 0;index < fileds.length;index++){
            String key = fileds[index];
            Object object = params.get(key);
            if(null != object){
                params.put(key,object.toString().toUpperCase());
            }
        }
    }
}
